package com.sky.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ModelUtils {
  // 是否需要输出python运行错误信息
  private static boolean errorFlag=false;
  // 替换为虚拟环境中的 Python 解释器路径
  public static String pythonPath="D:\\pycharm\\DataAnalyze\\Scripts\\python.exe";
  // python预测程序的路径
  public static String pythonProgramPredictPath="E:\\TakeOut\\Geers\\sky-common\\src\\main\\resources\\program\\predict.py";
  // 初始数据的文件夹
  public static String originDataPath="E:\\TakeOut\\Geers\\sky-common\\src\\main\\resources\\data\\";

  // 结果数据文件夹
  public static String resultDataPath="E:\\TakeOut\\Geers\\sky-common\\src\\main\\resources\\res\\";

  public static int countLines(String filePath) {
    // filePath是绝对路径
    int lineCount = 0;

    try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
      while (reader.readLine() != null) {
        lineCount++;
      }
    } catch (IOException e) {
      System.err.println("Error reading file: " + e.getMessage());
    }

    return lineCount;
  }
  public static String getResFileName(String fileName){
    // 输入初始数据文件名，输出 结果数据文件名
    int dotIndex = fileName.lastIndexOf(".");

    if (dotIndex != -1) {
      // 在点之前插入 "_res"
      String resFileName = fileName.substring(0, dotIndex) + "_res" + fileName.substring(dotIndex);
      return resFileName;
    } else {
      // 如果没有找到点，则直接在末尾添加 "_res"
      return fileName + "_res";
    }
  }

  public static void main(String[] args) {
    System.out.println(countLines(originDataPath + "test.csv"));

  }

  public static void predictData(String fileName , float limit) {

    try {
      // 构建命令
      String[] cmd = new String[5];
      cmd[0] = pythonPath ;
      cmd[1] = pythonProgramPredictPath;
      cmd[2] = originDataPath + fileName;
      cmd[3] = resultDataPath + getResFileName(fileName);
      cmd[4] = String.valueOf(limit);
      // 执行Python脚本
      Process process = Runtime.getRuntime().exec(cmd);
      if(errorFlag == true){
        String prediction = null;
        // 读取Python脚本的输出
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

        while ((prediction = stdInput.readLine()) != null) {
          System.out.println("Prediction: " + prediction);
        }
        // 读取错误流（如果有错误）
        BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        String s = null;
        while ((s = stdError.readLine()) != null) {
          System.out.println("Error: " + s);
        }
      }
      // 等待进程结束
      process.waitFor();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static boolean checkFileExist(String fileName){
    // 判断文件是否存在，fileName需要是相对路径
    // 创建 File 对象
    File file = new File(resultDataPath + fileName);
    // 判断文件是否存在
    if (file.exists() && file.length() !=0) {
      return true;
    }else{
      return false;
    }
  }
}
